<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <div slot="header" class="clearfix">

          <el-row>
            <el-button type="primary" @click.native="dialogVisible=true">+新增角色</el-button>
          </el-row>
        </div>
        <el-table
          :data="tableData"
          style="width: 100%"
        >
          <el-table-column
            label="序号"
            width="300"
          >
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="角色名称"
            width="300"
          >
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="描述"
            width="300"
          >
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.description }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
              >分配权限</el-button>
              <el-button
                size="mini"
                @click.native="updataUser(scope.row.id)"
              >编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                @click.native="delRoleid(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center" align="middle" style="height:60px">
          <el-pagination
            layout="prev, pager, next"
            :page-size="params.pagesize"
            :current-page="params.page"
            :total="total"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
      <el-dialog
        :title="`${user.id?'编辑':'添加'}角色`"
        :visible.sync="dialogVisible"
        width="30%"
        @close="closed"
      >
        <template #footer>
          <el-form ref="form" label-width="80px">
            <el-form-item label="角色名称">
              <el-input v-model="user.name" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="user.description" />
            </el-form-item>
          </el-form>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click.native="addRoles(user)">确 定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, delRole, addRole, getRolecontent, reviseRolecontent } from '@/api/setting'
export default {
  data() {
    return {
      tableData: [],

      // 分页数据
      params: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      dialogVisible: false,

      // 添加角色要提交的数据
      user: {
        name: '',
        description: ''
      }
    }
  },
  mounted() {
    this.fetchRoleList()
  },
  methods: {

    // 分页处理
    pageChange(page) {
      this.params.page = page
      this.fetchRoleList()
    },

    // 渲染页面
    async fetchRoleList() {
      const { rows, total } = await getRoleList(this.params)
      this.tableData = rows
      this.total = total
    },
    // 删除角色
    async delRoleid(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRole(id)
        this.$message.success('删除角色成功')
        this.fetchRoleList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加或修改角色
    async addRoles(data) {
      if (this.user.id) {
        await reviseRolecontent(data)
      } else {
        await addRole(data)
      }
      this.$message.success(`${this.user.id ? '编辑' : '添加'}角色成功`)
      this.dialogVisible = false
      this.fetchRoleList()
    },
    // 关闭对话框清空数据
    closed() {
      this.user = {
        name: '',
        description: '',
        id: ''
      }
    },
    // 编辑修改角色
    async updataUser(id) {
      const res = await getRolecontent(id)
      this.user = res
      this.dialogVisible = true
    }
  }
}
</script>
